package com.geralt.im.servercommand.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.geralt.im.model.NeedReplyMessage;
import com.geralt.im.model.ServerInfo;
import com.geralt.im.server.IMServer;
import com.geralt.im.server.IMServerClient;
import com.geralt.im.servercommand.ServerCommand;
import com.geralt.im.tool.JsonTool;

public class GetServerMessageCommand extends ServerCommand {
	SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH-mm-ss");
	private final String RUNNING = "RUNNING";
	private final String STOP = "STOP";

	@Override
	public String excute() {
		String serverStatus = null;
		String RunTimeLong = "0";
		ServerInfo serverInfo = new ServerInfo();
		Date startTime = new Date(IMServerClient.server.getStartTime());
		serverInfo.setStartTime(sdf.format(startTime));
		if (IMServerClient.server.isRun()) {
			serverStatus = RUNNING;
			RunTimeLong = System.currentTimeMillis()
					- IMServerClient.server.getStartTime() + "";
		} else {
			serverStatus = STOP;
		}
		serverInfo.setRunTimeLong(RunTimeLong);
		List<String> users = new ArrayList<String>();
		users.addAll(IMServer.map.keySet());
		serverInfo.setOnlineUsers(users);
		JsonTool jt = JsonTool.getInstance();
		serverInfo.setServerStatus(serverStatus);
		System.out.println(serverInfo);
		String jsonString = jt.objectToString(serverInfo);
		System.out.println(jsonString);
		sendReply(new NeedReplyMessage(jsonString, uuid));

		return jsonString;
	}

}
